<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['consume:consume:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['consume:consume:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['consume:consume:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['consume:consume:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="consumeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="itemId" />
      <el-table-column label="项目名称" align="center" prop="itemName" />
      <el-table-column label="所属分类" align="center" prop="typeId" />
      <el-table-column label="技师数量" align="center" prop="artificerNum" />
      <el-table-column label="轮钟单价" align="center" prop="arrangePrice" />
      <el-table-column label="选钟单价" align="center" prop="choosePrice" />
      <el-table-column label="报钟器是否显示" align="center" prop="showFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.showFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="助记码" align="center" prop="mnemonic" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no_enable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['consume:consume:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['consume:consume:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">

        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="基本信息" name="first">
            <el-form-item label="项目名称" prop="itemName">
              <el-input v-model="form.itemName" placeholder="请输入项目名称" />
            </el-form-item>
            <el-form-item label="所属分类" prop="typeId">
              <el-select v-model="form.typeId" placeholder="请选择">
                <el-option
                  v-for="item in consumeTypes"
                  :key="item.typeId"
                  :label="item.typeName"
                  :value="item.typeId">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="技师数量" prop="artificerNum">
              <el-input v-model="form.artificerNum" placeholder="请输入技师数量" />
            </el-form-item>
            <el-form-item label="是否计时品项" prop="needTimekeeping">
              <el-radio-group v-model="form.needTimekeeping">
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="轮钟时长" prop="arrangeTime">
              <el-input v-model="form.arrangeTime" placeholder="请输入轮钟时长" />
            </el-form-item>
            <el-form-item label="轮钟单价" prop="arrangePrice">
              <el-input v-model="form.arrangePrice" placeholder="请输入轮钟单价" />
            </el-form-item>
            <el-form-item label="点钟时长" prop="appointTime">
              <el-input v-model="form.appointTime" placeholder="请输入点钟时长" />
            </el-form-item>
            <el-form-item label="点钟单价" prop="appointPrice">
              <el-input v-model="form.appointPrice" placeholder="请输入点钟单价" />
            </el-form-item>
            <el-form-item label="加钟时长" prop="addclockTime">
              <el-input v-model="form.addclockTime" placeholder="请输入加钟时长" />
            </el-form-item>
            <el-form-item label="加钟单价" prop="addclockPrice">
              <el-input v-model="form.addclockPrice" placeholder="请输入加钟单价" />
            </el-form-item>
            <el-form-item label="选钟时长" prop="chooseTime">
              <el-input v-model="form.chooseTime" placeholder="请输入选钟时长" />
            </el-form-item>
            <el-form-item label="选钟单价" prop="choosePrice">
              <el-input v-model="form.choosePrice" placeholder="请输入选钟单价" />
            </el-form-item>
            <el-form-item label="Call钟时长" prop="callTime">
              <el-input v-model="form.callTime" placeholder="请输入Call钟时长" />
            </el-form-item>
            <el-form-item label="Call钟单价" prop="callPrice">
              <el-input v-model="form.callPrice" placeholder="请输入Call钟单价" />
            </el-form-item>
            <el-form-item label="声音文件名称" prop="voiceName">
              <el-input v-model="form.voiceName" placeholder="请输入声音文件名称" />
            </el-form-item>
            <el-form-item label="允许提前下钟时间" prop="beforeTime">
              <el-input v-model="form.beforeTime" placeholder="请输入允许提前下钟时间" />
            </el-form-item>
            <el-form-item label="轮钟是否动牌" prop="moveFlag">
              <el-radio-group v-model="form.moveFlag">
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="报钟器是否显示" prop="showFlag">
              <el-radio-group v-model="form.showFlag">
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="助记码" prop="mnemonic">
              <el-input v-model="form.mnemonic" placeholder="请输入助记码" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_yes_no_enable"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="排序" prop="sort">
              <el-input v-model="form.sort" placeholder="请输入排序" />
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="关联技师" name="second">


          </el-tab-pane>
          <el-tab-pane label="关联商品" name="third">关联商品</el-tab-pane>
        </el-tabs>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConsume, getConsume, delConsume, addConsume, updateConsume } from "@/api/consume/consume";
import { getAllConsumeType } from "@/api/goods/consumeType";

export default {
  name: "Consume",
  dicts: ['sys_yes_no_enable', 'sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目管理表格数据
      consumeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemName: null,
      },
      consumeTypes:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        itemName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        typeId: [
          { required: true, message: "所属分类不能为空", trigger: "change" }
        ],
        artificerNum: [
          { required: true, message: "技师数量不能为空", trigger: "blur" }
        ],
        arrangeTime: [
          { required: true, message: "轮钟时长不能为空", trigger: "blur" }
        ],
        arrangePrice: [
          { required: true, message: "轮钟单价不能为空", trigger: "blur" }
        ],
        appointTime: [
          { required: true, message: "点钟时长不能为空", trigger: "blur" }
        ],
        appointPrice: [
          { required: true, message: "点钟单价不能为空", trigger: "blur" }
        ],
        addclockTime: [
          { required: true, message: "加钟时长不能为空", trigger: "blur" }
        ],
        addclockPrice: [
          { required: true, message: "加钟单价不能为空", trigger: "blur" }
        ],
        chooseTime: [
          { required: true, message: "选钟时长不能为空", trigger: "blur" }
        ],
        choosePrice: [
          { required: true, message: "选钟单价不能为空", trigger: "blur" }
        ],
        callTime: [
          { required: true, message: "Call钟时长不能为空", trigger: "blur" }
        ],
        callPrice: [
          { required: true, message: "Call钟单价不能为空", trigger: "blur" }
        ],
        voiceName: [
          { required: true, message: "声音文件名称不能为空", trigger: "blur" }
        ],
        beforeTime: [
          { required: true, message: "允许提前下钟时间不能为空", trigger: "blur" }
        ],
        mnemonic: [
          { required: true, message: "助记码不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      },
      consumeTypePrimary:{typeCode:"project"},
      activeName:'first'
    };
  },
  created() {
    this.getList();
    this.getGoodsConsumeType(this.consumeTypePrimary);
  },
  methods: {
    /** 查询项目管理列表 */
    getList() {
      this.loading = true;
      listConsume(this.queryParams).then(response => {
        this.consumeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getGoodsConsumeType(query){
      getAllConsumeType(query).then(response => {
        if(response.code == 200){
          this.consumeTypes = response.data;
        }else{
          this.$modal.msgError(response.msg);
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        itemId: null,
        itemName: null,
        typeId: null,
        artificerNum: null,
        needTimekeeping: null,
        arrangeTime: null,
        arrangePrice: null,
        appointTime: null,
        appointPrice: null,
        addclockTime: null,
        addclockPrice: null,
        chooseTime: null,
        choosePrice: null,
        callTime: null,
        callPrice: null,
        voiceName: null,
        beforeTime: null,
        moveFlag: null,
        showFlag: null,
        mnemonic: null,
        status: null,
        createTime: null,
        sort: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.itemId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const itemId = row.itemId || this.ids
      getConsume(itemId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.itemId != null) {
            updateConsume(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConsume(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const itemIds = row.itemId || this.ids;
      this.$modal.confirm('是否确认删除项目管理编号为"' + itemIds + '"的数据项？').then(function() {
        return delConsume(itemIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('consume/consume/export', {
        ...this.queryParams
      }, `consume_${new Date().getTime()}.xlsx`)
    },
    handleClick(tab, event) {
      console.log(tab, event);
    }
  }
};
</script>
